#include<stdio.h>
#include<stdlib.h>
#include"queue.h"

uint8_t create_queue(queue_t *pQ, uint8_t *pBuf, int maxSize)
{
	if((pQ == NULL) || (pBuf == NULL))
	{
		return 0;
	}
	
	pQ->pBuf = pBuf;
	pQ->maxSize = maxSize;
	pQ->front = 0;
	pQ->rear = 0;
}

uint8_t is_queue_full(queue_t *pQ)
{
	if(pQ->front == ((pQ->rear + 1) % pQ->maxSize))
		return 1;
	else
		return 0;
}

uint8_t is_queue_empty(queue_t *pQ)
{
	if(pQ->front == pQ->rear)
		return 1;
	else
		return 0;
}

int add_queue_data(queue_t *pQ, uint8_t *pData, int len)
{
	int i = 0;
	if((pData == NULL) || (pQ == NULL))
		return -1;

	for(i = 0; i < len; i++)
	{
		if(is_full(pQ))
		{
			return i;
		}
		pQ->pBuf[pQ->rear] = pData[i];
		pQ->rear = (pQ->rear + 1) % pQ->maxSize;
	}
}

int get_queue_data(queue_t *pQ, uint8_t *pData, int len)
{
	int i = 0;
	if(pData == NULL)
		return -1;
	
	for(i = 0; i < len; i++)
	{
		if(is_empty(pQ))
		{
			return i;
		}
		pData[i] = pQ->pBuf[pQ->front];
		pQ->front = (pQ->front + 1) % pQ->maxSize;
	}
}



